package com.example.zyfyptlzx729.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyfyptlzx729.R;
import com.example.zyfyptlzx729.activities.ViewPDFActivity;
import com.example.zyfyptlzx729.bean.TwareBean;
import com.example.zyfyptlzx729.common.Common;
import com.squareup.picasso.Picasso;

import java.util.List;

public class TwareAdapter extends RecyclerView.Adapter {
    private List<TwareBean> list;
    private Context context;
    private LayoutInflater layoutInflater;
    public TwareAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView tvtile,tvauthor,tvtime;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imageView2);
            tvtile=itemView.findViewById(R.id.textView4);
            tvauthor=itemView.findViewById(R.id.textView8);
            tvtime=itemView.findViewById(R.id.textView10);
        }
    }
    public void setList(List<TwareBean>list){
        this.list=list;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final TwareBean bean=list.get(position);
        if(bean==null)
            return;
        ViewHolder viewHolder=(ViewHolder)holder;
        Picasso.with(context)
                .load(Common.IMAGEURL+bean.getThumb())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);
        viewHolder.tvtile.setText(bean.getName());
//        viewHolder.tvauthor.setText(bean.getAuthor());
//        viewHolder.tvauthor.setText(bean.getAuthor());
        viewHolder.tvtime.setText(bean.getUpdate_time());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id=bean.getId();
                Toast.makeText(context,""+id,Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(context, ViewPDFActivity.class);
                intent.putExtra("resid",bean.getId());
                intent.putExtra("title",bean.getName());
                intent.putExtra("authorid",bean.getUserid());
                intent.putExtra("pdfattach",bean.getPdfattach());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(list!=null)
            return list.size();
        else
            return 0;
    }
}
